package com.hmall.search.task;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.hmall.search.dto.Item;
import com.hmall.search.mapper.SearchMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 商品预热
 * @Description CommodityPreheating
 * @Author dmx
 * @Date 2024-11-15
 */
@Component
@Slf4j
public class ItemTask {

    @Autowired
    private SearchMapper searchMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;
    //CommodityPreheating
    //商品预热定时任务
    @Scheduled(cron = "0 20 20 * * ?")
    public void commodityPreheatingTask(){
        //1.查询销量前20的商品
        List<Item> itemList=searchMapper.selectTopTwentieth();
        //2.将商品存储到redis中"itemList::", JSONUtil.toJsonStr(itemList)
        redisTemplate.opsForValue().set("itemList::",JSONUtil.toJsonStr(itemList));

    }

}
